<?php
if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

function checkautocloseshop($products) {
	global $_G;

	if(!$_G['shops']['ismoderator'] && $_G['shops']['autoclose']) {
		$closedby = $_G['shops']['autoclose'] > 0 ? 'dateline' : 'lastpost';
		$_G['shops']['autoclose'] = abs($_G['shops']['autoclose']);
		if(TIMESTAMP - $thread[$closedby] > $_G['shops']['autoclose'] * 86400) {
			return 'post_products_closed_by_'.$closedby;
		}
	}
	return FALSE;
}

function shop(&$shop) {
	global $_G;
	$lastvisit = $_G['member']['lastvisit'];
	if(!$shop['viewperm'] || ($shop['viewperm'] && forumperm($shop['viewperm'])) || !empty($shop['allowview']) || (isset($shop['users']) && strstr($shop['users'], "\t$_G[uid]\t"))) {
		$shop['permission'] = 2;
	} elseif(!$_G['setting']['hideprivate']) {
		$shop['permission'] = 1;
	} else {
		return FALSE;
	}

	if($shop['icon']) {
		$shop['icon'] = get_shopimg($shop['icon']);
		$shop['icon'] = '<a href="shop.php?sid='.$shop['sid'].'"><img src="'.$shop['icon'].'" align="left" alt="" /></a>';
	}

	$lastpost = array(0, 0, '', '');

	$shop['lastpost'] = is_string($shop['lastpost']) ? explode("\t", $shop['lastpost']) : $shop['lastpost'];

	$shop['lastpost'] =count($shop['lastpost']) != 4 ? $lastpost : $shop['lastpost'];

	list($lastpost['tid'], $lastpost['subject'], $lastpost['dateline'], $lastpost['author']) = $shop['lastpost'];

	$shop['folder'] = (isset($_G['cookie']['sid'.$shop['sid']]) && $_G['cookie']['sid'.$shop['sid']] > $lastvisit ? $_G['cookie']['sid'.$shop['sid']] : $lastvisit) < $lastpost['dateline'] ? ' class="new"' : '';

	if($lastpost['tid']) {
		$lastpost['dateline'] = dgmdate($lastpost['dateline'], 'u');
		$lastpost['authorusername'] = $lastpost['author'];
		if($lastpost['author']) {
			$lastpost['author'] = '<a href="home.php?mod=space&username='.rawurlencode($lastpost['author']).'">'.$lastpost['author'].'</a>';
		}
		$shop['lastpost'] = $lastpost;
	} else {
		$shop['lastpost'] = $lastpost['authorusername'] = '';
	}

	$shop['moderators'] = moddisplay($shop['moderators'], $_G['setting']['moddisplay'], !empty($shop['inheritedmod']));

	if(isset($shop['subforums'])) {
		$shop['subforums'] = implode(', ', $shop['subforums']);
	}

	return TRUE;
}

function shopselect($groupselectable = FALSE, $arrayformat = 0, $selectedfid = 0, $showhide = FALSE, $evalue = FALSE, $special = 0) {
	global $_G;

	if(!isset($_G['cache']['shops'])) {
		loadcache('shops');
	}
	$shopcache = &$_G['cache']['shops'];
	$shoplist = $arrayformat ? array() : '<optgroup label="&nbsp;">';
	foreach($shopcache as $shop) {
		if(!$shop['status'] && !$showhide) {
			continue;
		}
		if($selectedfid) {
			if(!is_array($selectedfid)) {
				$selected = $selectedfid == $shop['sid'] ? ' selected' : '';
			} else {
				$selected = in_array($shop['sid'], $selectedfid) ? ' selected' : '';
			}
		}
		if($shop['type'] == 'group') {
			if($arrayformat) {
				$shoplist[$shop['sid']]['name'] = $shop['name'];
			} else {
				$shoplist .= $groupselectable ? '<option value="'.($evalue ? 'gid_' : '').$shop['sid'].'" class="bold">--'.$shop['name'].'</option>' : '</optgroup><optgroup label="--'.$shop['name'].'">';
			}
			$visible[$shop['sid']] = true;
		} elseif($shop['type'] == 'shop' && isset($visible[$shop['sup']]) && (!$shop['viewperm'] || ($shop['viewperm'] && forumperm($shop['viewperm'])) || strstr($shop['users'], "\t$_G[uid]\t"))) {
			if($arrayformat) {
				$shoplist[$shop['sup']]['sub'][$shop['sid']] = $shop['name'];
			} else {
				$shoplist .= '<option value="'.($evalue ? 'sid_' : '').$shop['sid'].'"'.$selected.'>'.$shop['name'].'</option>';
			}
			$visible[$shop['sid']] = true;
		} elseif(!$arrayformat && $shop['type'] == 'sub' && isset($visible[$shop['sup']]) && (!$shop['viewperm'] || ($shop['viewperm'] && forumperm($shop['viewperm'])) || strstr($shop['users'], "\t$_G[uid]\t"))) {
			$shoplist .= '<option value="'.($evalue ? 'sid_' : '').$shop['sid'].'"'.$selected.'>&nbsp; &nbsp; &nbsp; '.$shop['name'].'</option>';
			
		}
	}
	if(!$arrayformat) {
		$shoplist .= '</optgroup>';
		$shoplist = str_replace('<optgroup label="&nbsp;"></optgroup>', '', $shoplist);
	}

	return $shoplist;
}

function getcacheinfoshop($tid) {
	global $_G;
	$tid = intval($tid);
	$cachethreaddir2 = DISCUZ_ROOT.'/'.$_G['setting']['cachethreaddir'];
	$cache = array('filemtime' => 0, 'filename' => '');
	$tidmd5 = substr(md5($tid), 3);
	$fulldir = $cachethreaddir2.'/'.$tidmd5[0].'/'.$tidmd5[1].'/'.$tidmd5[2].'/';
	$cache['filename'] = $fulldir.$tid.'.htm';
	if(file_exists($cache['filename'])) {
		$cache['filemtime'] = filemtime($cache['filename']);
	} else {
		if(!is_dir($fulldir)) {
			dmkdir($fulldir);
		}
	}
	return $cache;
}

function get_shopimg($imgname) {
	global $_G;
	if($imgname) {
		$parse = parse_url($imgname);
		if(isset($parse['host'])) {
			$imgpath = $imgname;
		} else {
			if($_G['shops']['status'] != 3) {
				$imgpath = $_G['setting']['attachurl'].'common/'.$imgname;
			} else {
				$imgpath = $_G['setting']['attachurl'].'group/'.$imgname;
			}
		}
		return $imgpath;
	}
}

function shopleftside() {
	global $_G;
	$leftside = array('favorites' => array(), 'shops' => array());
	$leftside['shops'] = shopselect(FALSE, 1);
	$_G['leftsidewidth_mwidth'] = $_G['setting']['leftsidewidth'] + 15;
	return $leftside;
}

?>